
from django.utils import timezone
from core.models.log import OperationLog

def log_operation(user, operation_type, content_type, object_id, object_name, details='', ip_address=''):
    """
    记录用户操作日志
    :param user: 操作用户
    :param operation_type: 操作类型
    :param content_type: 对象类型
    :param object_id: 对象ID
    :param object_name: 对象名称
    :param details: 操作详情
    :param ip_address: IP地址
    :return: 操作日志对象
    """
    log = OperationLog.objects.create(
        user=user,
        operation_type=operation_type,
        content_type=content_type,
        object_id=object_id,
        object_name=object_name,
        timestamp=timezone.now(),
        details=details,
        ip_address=ip_address
    )
    return log